﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Web.Security;
using System.Xml;

namespace HaiTaoTools
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //MD5加密函数
        public string md5(string str, int code)
        {
            if (code == 16) //16位MD5加密（取32位加密的9~25字符） 
            {

                return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower().Substring(8, 16);
            }
            else//32位加密 
            {
                return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower();
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            labDateTime.Text = DateTime.Now.ToString();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            string link = linkLabel1.Text;
            System.Diagnostics.Process.Start(link);
        }

        private void btnok_Click(object sender, EventArgs e)
        {
            string strin = BoxIn.Text;
            string strto;
            if (strin != "")
            {
                if (RdBtn32.Checked == true)
                {
                    strto = md5(strin, 32);
                }
                else
                {
                    strto = md5(strin, 16);
                }
                if (ChkBoxUpper.Checked == true)
                {
                    strto = strto.ToUpper();
                }
                BoxTo.Text = strto;
            }
        }

        private void BoxIn_KeyUp(object sender, KeyEventArgs e)
        {
            btnok_Click(sender, e);
        }

        private void BtnWhios_Click(object sender, EventArgs e)
        {
            string url = TBUrl.Text.Trim();
            if (!string.IsNullOrEmpty(url))
            {
                try
                {

                    string reWhios = Whios.GetWhios(url);
                    int pos = reWhios.IndexOf("友情提示"); //开始位置 
                    if (pos > 0)
                        reWhios = reWhios.Substring(0, pos);
                    RTBWhios.Text = reWhios;
                }
                catch (Exception ex)
                {
                    RTBWhios.Text = ex.Message;
                }
            }
        }

        private void BtnWeather_Click(object sender, EventArgs e)
        {
            string city = TBCity.Text.Trim();
            if (!string.IsNullOrEmpty(city))
            {
                RTBWeather.Clear();
                XmlNodeList ggNodeList;
                //获取当天天气信息
                ggNodeList = Weather.GoogleWeatherAPI(city).SelectNodes("xml_api_reply/weather/current_conditions");
                if (ggNodeList.Count <= 0)
                {
                    RTBWeather.Text = "未找到此城市！";
                    return;
                }
                RTBWeather.Text += ggNodeList.Item(0).SelectSingleNode("temp_c").Attributes["data"].InnerText + "°C\r\n";//获取温度信息
                RTBWeather.Text += ggNodeList.Item(0).SelectSingleNode("humidity").Attributes["data"].InnerText + "\r\n";//获取湿度信息
                RTBWeather.Text += ggNodeList.Item(0).SelectSingleNode("condition").Attributes["data"].InnerText + "\r\n";//获取天气状况信息
                RTBWeather.Text += ggNodeList.Item(0).SelectSingleNode("wind_condition").Attributes["data"].InnerText + "\r\n\r\n";//获取风向信息
                //获取远程图片的url地址，进行组装        
                string picPath = "http://www.google.com" + ggNodeList.Item(0).SelectSingleNode("icon").Attributes["data"].InnerText;//获取远程图片地址
                picBoxWeather.Image = Weather.GetPicImage(picPath);//通过GetPicImage方法，将远程图片转化为.net CF图片控件所能使用的Image对象


                //获取后四天天气信息
                ggNodeList = Weather.GoogleWeatherAPI(city).SelectNodes("xml_api_reply/weather/forecast_conditions");
                for (int i = 0; i < 3; i++)
                {
                    RTBWeather.Text += ggNodeList.Item(i).SelectSingleNode("day_of_week").Attributes["data"].InnerText + "\r\n";//获取星期
                    RTBWeather.Text += ggNodeList.Item(i).SelectSingleNode("low").Attributes["data"].InnerText + "°C\r\n";//获取最低温度
                    RTBWeather.Text += ggNodeList.Item(i).SelectSingleNode("high").Attributes["data"].InnerText + "°C\r\n";//获取最高温度
                    RTBWeather.Text += ggNodeList.Item(i).SelectSingleNode("condition").Attributes["data"].InnerText + "\r\n\r\n";//获取天气状况信息
                }

            }
        }

    }
}
